﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ProductPlanPrint
{
    public partial class FinishPlanPrint : Form
    {
        Dictionary<string, string> pars;
        DBHelper db;
        HttpAgreement http;
        public FinishPlanPrint(Dictionary<string, string> dic)
        {
            InitializeComponent();
            db = new DBHelper();
            pars = dic;
        }

        private void FinishPlanPrint_Load(object sender, EventArgs e)
        {
            // 加载模板
            CommonMethods.loadPrintTemplte(cb_0_Template);
            // 加载打印机
            CommonMethods.loadPrintDevice(cb_0_Print);
        }

        private void btn_print_Click(object sender, EventArgs e)
        {
            var json = JsonConvert.DeserializeObject<JObject>(pars["pars"]);

            var plCode = json["plCode"].ToString();

            var headDt = GetHeadDt(plCode);
            var bodyDt = GetBodyDt(plCode);

            var printDt = new DataTable();

            // 表头信息
            printDt.Columns.Add("生产订单编号");
            printDt.Columns.Add("下单日期");
            printDt.Columns.Add("表头备注");

            printDt.Columns.Add("短溢");
            printDt.Columns.Add("零卷范围");
            printDt.Columns.Add("整卷率");
            printDt.Columns.Add("珍珠棉");
            printDt.Columns.Add("牛皮纸");

            printDt.Columns.Add("纸筒");
            printDt.Columns.Add("纸管");
            printDt.Columns.Add("壁厚");

            printDt.Columns.Add("交付日期");

            // 表体信息
            printDt.Columns.Add("产品名称");
            printDt.Columns.Add("色号");
            printDt.Columns.Add("网布");
            printDt.Columns.Add("工艺");
            printDt.Columns.Add("克重/厚度");

            printDt.Columns.Add("宽幅");
            printDt.Columns.Add("卷长");
            printDt.Columns.Add("数量");
            printDt.Columns.Add("纹路");
            printDt.Columns.Add("钙粉");

            printDt.Columns.Add("PHR");
            printDt.Columns.Add("阻燃");
            printDt.Columns.Add("防寒");
            printDt.Columns.Add("防霉");
            printDt.Columns.Add("UV");

            printDt.Columns.Add("备注");

            // 表头取值
            var headRow = headDt.Rows[0];

            var 生产订单编号 = headRow["plCode"].ToString();
            var 下单日期 = headRow["createtime"].ToString();
            var 表头备注 = headRow["memo"].ToString();

            var 短溢 = headRow["cu_shortOverFlow"].ToString();
            var 零卷范围 = headRow["cu_zeroRollRange"].ToString();
            var 整卷率 = headRow["cu_fullRollRate"].ToString();
            var 珍珠棉 = headRow["cu_pearlCotton"].ToString();
            var 牛皮纸 = headRow["cu_kraftPaper"].ToString();

            var 纸筒 = headRow["cu_paperCylinder"].ToString();
            var 纸管 = headRow["cu_paperTube"].ToString();
            var 壁厚 = headRow["cu_wallThickness"].ToString();

            var 交付日期 = headRow["payDate"].ToString();

            foreach (DataRow row in bodyDt.Rows)
            {
                var printRow = printDt.NewRow();

                // 表头
                printRow["生产订单编号"] = 生产订单编号;
                printRow["下单日期"] = 下单日期;
                printRow["表头备注"] = 表头备注;

                printRow["短溢"] = 短溢;
                printRow["零卷范围"] = 零卷范围;
                printRow["整卷率"] = 整卷率;
                printRow["珍珠棉"] = 珍珠棉 == "on" ? "有" : "无";
                printRow["牛皮纸"] = 牛皮纸 == "on" ? "有" : "无";

                printRow["纸筒"] = 纸筒 == "on" ? "有" : "无";
                printRow["纸管"] = 纸管 == "on" ? "有" : "无";
                printRow["壁厚"] = 壁厚;

                printRow["交付日期"] = 交付日期;

                // 表体
                printRow["产品名称"] = row["cu_proName"].ToString();
                printRow["色号"] = row["cu_colorCode"].ToString();
                printRow["网布"] = row["cu_netCloth"].ToString();
                printRow["工艺"] = row["cu_craft"].ToString();
                printRow["克重/厚度"] = row["cu_gramThick"].ToString();

                printRow["宽幅"] = row["cu_widthRange"].ToString();
                printRow["卷长"] = row["cu_rollLength"].ToString();
                printRow["数量"] = row["cu_numberOfPieces"].ToString();
                printRow["纹路"] = row["cu_texture"].ToString();
                printRow["钙粉"] = row["cu_calciumPowder"].ToString();

                printRow["PHR"] = row["cu_PHR"].ToString();
                printRow["阻燃"] = row["cu_flameRetardant"].ToString();
                printRow["防寒"] = row["cu_coldProtection"].ToString();
                printRow["防霉"] = row["cu_shockProof"].ToString();
                printRow["UV"] = row["cu_UV"].ToString();

                printRow["UV"] = row["cu_memo"].ToString();

                printDt.Rows.Add(printRow);
            }
            var info = "";
            var pResult = CommonMethods.DoPrint_horizontal(printDt, cb_0_Template.Text, cb_0_Print.Text, ref info);
        }

        DataTable GetHeadDt(string plCode)
        {
            var sql = "select * from V_A1_ProducePlanMainList where plCode = '" + plCode + "' ";
            return db.getData(sql, "");
        }

        DataTable GetBodyDt(string plCode)
        {
            var sql = "  select * from V_A2_ProducePlanChildList where plCode = '" + plCode + "' ";
            return db.getData(sql, "");
        }
    }
}
